package tasks

import (
	"context"
	"fly/service"
	"github.com/beego/beego/v2/core/logs"
	"github.com/beego/beego/v2/task"
	"runtime/debug"
)

// 定时任务
//       second: 0-59
//       minute: 0-59
//       hour: 1-23
//       day: 1-31
//       month: 1-12
//       weekdays: 0-6（0 is Sunday）

// AircraftUpdateTask 添加更新飞机任务
func AircraftUpdateTask() (tName string, tTask *task.Task) {
	tName = "aircraftUpdateTask"
	tTask = task.NewTask(tName, "*/30 * * * *", func(ctx context.Context) error {
		defer func() {
			if err := recover(); err != nil {
				s := string(debug.Stack())
				logs.Error("定时任务异常:cause>> %+v >>> stack:%v", err, s)
			}
		}()
		var aircraftService service.AircraftService
		err1 := aircraftService.UpdateAllAircraftStatus()
		// 前端手动更新任务失效状态
		//var flightTaskService service.FlightTaskService
		//err2 := flightTaskService.UpdateAllTaskStatus()
		//return condition.TernaryOperator(err1 != nil, err1, err2)
		return err1
	})
	return
}
